#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

from flask import Response
from flask_restful import Resource
from src.routers.GetDataFromManager import GetTileFromSLRC
sys.path.append("../..")

# 全局变量
# mapServiceManager = MapServiceManager.mapServiceManager

class ESRIBundle_tile(Resource):
    def __init__(self):
        print('init BundleServer')
    def get(self, servicename, level, row, column):
        resp = ''
        try:
            tile = GetTileFromSLRC(servicename, level, row, column)
            resp = Response(tile['imgData'], status=200, mimetype='image/png')
        except Exception as e:
            print('esmt_t_error')
            print(e)
            resp = Response('MapServer Not Found' +
                            '\n\nlevel:' + str(level) +
                            '\nrow:' + str(row) +
                            '\ncolumn:' + str(column),
                            status=404,
                            mimetype='text/javascript')
        finally:
            resp.headers['Access-Control-Allow-Origin'] = '*'
            resp.headers['Access-Control-Allow-Headers'] = 'Content-Type'
            resp.headers['Access-Control-Allow-Credentials'] = 'GET,POST,OPTIONS'
            return resp
    def getFromRedis(self, servicename, level, row, column):
        return ' '